﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using InformationSystem.Utility;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using InformationSystem.BLL.Core.Model;
using System.Text;
using System.Xml;
using InformationSystem.BLL.Core.Service;

namespace InformationSystem.WebSite.Module.SysMgr
{
    public partial class SysMgrMain : System.Web.UI.Page
    {
        public ISystemService SystemService { get; set; }
        LoginUser user = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["LoginUser"] != null)
                user = (LoginUser)Session["LoginUser"];
            if (Application[Common.AppRegInfo] != null)
            {
                SysEnvironment myreg = (SysEnvironment)Application[Common.AppRegInfo];
                if(myreg .EndTime>DateTime.Now )
                    regstatus.Text = "已注册！";
                else
                    regstatus.Text = "已注册，但授权超期！";
            }
        }

        protected void Register_Click(object sender, DirectEventArgs arg)
        {
            if (user == null)
                return;
            if (user.LoginUserType != UserType.Admin) return;
            string regNo = txtregid.Text.Trim();
            if (regNo == string.Empty)
            {
                X.Msg.Show(new MessageBoxConfig
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO,
                    Title = "信息",
                    Message = "序列号不能为空！"
                });
                return;
            }
            if (!regfile.HasFile)
            {
                X.Msg.Show(new MessageBoxConfig
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO,
                    Title = "信息",
                    Message = "请选择授权文件！"
                });
                return;
            }
            byte[] data = regfile.FileBytes;

            byte[] sign = new byte[256];
            byte[] sec = new byte[data.Length - sign.Length];

            System.Buffer.BlockCopy(data, 0, sign, 0, 256);
            System.Buffer.BlockCopy(data, 256, sec, 0, sec.Length);

            //检查签名信息
            if (!MSecurity.RSAVerify(sec, sign))
            {
                X.Msg.Show(new MessageBoxConfig
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO,
                    Title = "信息",
                    Message = "授权文件认证失败，请确保不曾对之进行修改！"
                });
                return;
            }
            //解密
            string xmltxt = Encoding.ASCII.GetString(sec);

            xmltxt = MSecurity.Decrypt(xmltxt);
            if (xmltxt == string.Empty)
            {
                X.Msg.Show(new MessageBoxConfig
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO,
                    Title = "信息",
                    Message = "解密失败！"
                });
                return;
            }
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmltxt);
            bool isreg = doc.SelectSingleNode("/root/isreg").InnerText == "1" ? true : false;
            DateTime regtime = DateTime.Parse(doc.SelectSingleNode("/root/regtime").InnerText);
            string regid = doc.SelectSingleNode("/root/regid").InnerText;
            DateTime regendtime = DateTime.Parse(doc.SelectSingleNode("/root/end").InnerText);

            if (regid != regNo)
            {
                X.Msg.Show(new MessageBoxConfig
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO,
                    Title = "信息",
                    Message = "授权文件与安装序列号不匹配！"
                });
                return;
            }
            if (regendtime < DateTime.Now)
            {
                X.Msg.Show(new MessageBoxConfig
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO,
                    Title = "信息",
                    Message = "授权文件已经过期！"
                });
                return;
            }
            //存入数据库
            SysRegEnvironment sysreg = new SysRegEnvironment();
            sysreg.RegData = sec;
            sysreg.RegID = regNo;
            sysreg.RegTime = DateTime.Now;
            sysreg.VerifyData = sign;
            SystemService.RegisterSystem(sysreg);
            //更新App状态
            SysEnvironment sement = new SysEnvironment();
            sement.EndTime = regendtime;
            sement.IsRegisted = isreg;
            sement.RegisterGuid = regid;
            sement.RegisterTime = regtime;
            Application.Lock();
            Application[Common.AppRegInfo] = sement;
            Application.UnLock();
            regstatus.Text = "注册成功！";
            X.Msg.Show(new MessageBoxConfig
            {
                Buttons = MessageBox.Button.OK,
                Icon = MessageBox.Icon.INFO,
                Title = "信息",
                Message = "授权成功！"
            });
        }
    }
}